package com.zst.dollars.entity;

import com.zst.dollars.entity.connection.ClientDisconnectAnnounceMessage;
import com.zst.dollars.entity.connection.ServerConnectResultMessage;
import com.zst.dollars.entity.keepalive.KeepAliveResponseMessage;
import com.zst.dollars.entity.keepalive.KeepAliveRequestMessage;
import com.zst.dollars.entity.msg.ClientMsgRequestMessage;
import com.zst.dollars.entity.msg.ServerMsgBoardcastMessage;
import com.zst.dollars.entity.msg.ServerMsgResponseMessage;
import com.zst.dollars.entity.signin.ClientSignInAnnounceMessage;
import com.zst.dollars.entity.signin.ClientSignInRequestMessage;
import com.zst.dollars.entity.signin.ServerSignInResponseMessage;

import java.util.function.Predicate;

/**
 * 消息类型的枚举值
 */
public enum MessageType {
    CLIENT_DISCONNECT_ANNOUNCE(1, ClientDisconnectAnnounceMessage.class),
    SERVER_CONNECT_RESULT(2,ServerConnectResultMessage.class),
    CLIENT_KEEPALIVE_RESPONSE(3, KeepAliveResponseMessage.class),
    SERVER_KEEPALIVE_REQUEST(4, KeepAliveRequestMessage.class),
    CLIENT_MSG_REQUEST(5, ClientMsgRequestMessage.class),
    SERVER_MSG_BOARDCAST(6, ServerMsgBoardcastMessage.class),
    SERVER_MSG_RESPONSE(7, ServerMsgResponseMessage.class),
    CLIENT_SIGNIN_ANNOUNCE(8,ClientSignInAnnounceMessage.class),
    CLIENT_SIGNIN_REQUEST(9, ClientSignInRequestMessage.class),
    SERVER_SIGNIN_RESPONSE(10, ServerSignInResponseMessage.class);


    private int type;
    private Class<? extends Message> clazz;

    MessageType(int type, Class<? extends Message> clazz){
        this.type = type;
        this.clazz = clazz;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Class<? extends Message> getClazz() {
        return clazz;
    }

    public void setClazz(Class<? extends Message> clazz) {
        this.clazz = clazz;
    }

    public static Class<? extends Message> getClazzByType(int type){
        return findClazz(messageType -> messageType.getType() == type);
    }

    private static Class<? extends Message> findClazz(Predicate<MessageType> predicate){
        MessageType[] types = values();
        for(MessageType type : types){
            if(predicate.test(type)){
                return type.getClazz();
            }
        }

        throw new AssertionError("No Type Found");
    }
}
